$(function () {
    load();
    Time();
})

function Time() {
    var time = new Date();
    var y = time.getFullYear();
    var mon = time.getMonth() + 1; //0-11
    var d = time.getDate();
    var h = time.getHours();
    var m = time.getMinutes();
    var s = time.getSeconds();
    $("#datetime").text(y + "-" + mon + "-" + d + " " + h + ":" + m + ":" + s);
}

function fh() {
    localStorage.removeItem("mManufactureInsideSH(1)");
    localStorage.removeItem("mManufactureInsideSH(1)(01)");
    location.href = "../../wen_zhi_yi/mManufactureInsideSH(1).html";
}

function load() {
    let user = JSON.parse(localStorage.getItem("user"));
    $("#rname").text(user.rname);
    let mManufacture = JSON.parse(localStorage.getItem("mManufactureInsideSH"));
    $("#manufactureId").text(mManufacture.manufactureid);
    let mProcedure = JSON.parse(localStorage.getItem("mManufactureInsideSH(1)"));
    $("#procedureName").text(mProcedure.procedurename);
    $("#labourHourAmount").val(mProcedure.labourhouramount);
    $("#realLabourHourAmount").val(mProcedure.reallabourhouramount);

    let str = " <tr>\n" +
        "          <th>序号</th>\n" +
        "          <th>物料名称</th>\n" +
        "          <th>物料编号</th>\n" +
        "          <th>设计数量</th>\n" +
        "          <th>补充数量</th>\n" +
        "          <th>已使用数量</th>\n" +
        "          <th>本次数量</th>\n" +
        "      </tr>";
    $.each(mProcedure.MpLists, function (index, item) {
        str += " <tr>\n" +
            "      <td><span class='ids'>" + item.id + "</span></td>\n" +
            "      <td>" + item.productname + "</td>\n" +
            "      <td>" + item.productid + "</td>\n" +
            "      <td><span class='amount'>" + item.amount + "</span></td>\n" +
            "      <td>0.0</td>\n" +
            "      <td>0.0</td>\n" +
            "      <td><input type=\"number\" name=\"realAmount\" value='"+item.realamount+"' style=\"border: none\" class=\"xia\"></td>\n" +
            "  </tr>";
    })
    $("#tableMP01").html(str);
}

function sut() {
    let realLabourHourAmount = $("#realLabourHourAmount").val();
    if (realLabourHourAmount == "") {
        alert("工时数不能为空！")
        return;
    }
    if (parseInt(realLabourHourAmount)<0) {
        alert("工时数不能小于0！")
        return;
    }
    let shu = [];
    $("[name='realAmount']").each(function () {
        shu.push($(this).val());
    })
    let amount =[];
    $(".amount").each(function () {
        amount.push($(this).text());
    })
    for (let l = 0; l < shu.length; l++) {
        if (shu[l] == "") {
            alert("本次数量不能为空！");
            return;
        }
    }
    for (let l = 0; l < shu.length; l++) {
        if (parseInt(shu[l])<0) {
            alert("本次数量不能小于0！");
            return;
        }
    }
    for (let i=0;i<amount.length;i++){
        if (parseInt(amount[i])<parseInt(shu[i])){
            alert("本次数量不能大于设计数量！")
            return;
        }
    }

    let mManufacture = localStorage.getItem("mManufactureInsideSH");
    let mProcedure = localStorage.getItem("mManufactureInsideSH(1)");
    let id = JSON.stringify(shu);

    $.post("/Mmanufacture.do/MmanufactureDengJiSH", {
            realLabourHourAmount: realLabourHourAmount,
            mManufacture: mManufacture, mProcedure: mProcedure, shu: id
        },function (sdata) {
        $("#tishi").text("审核成功！")
        $("#tishi").css("color", "lime")
        $("#staticBackdrop").modal("show");
        })
}


function hh() {
    localStorage.removeItem("mManufactureInsideSH(1)");
    localStorage.removeItem("mManufactureInsideSH(1)(01)");
    location.href = "/wen_zhi_yi/mManufactureInsideSH.html";
}



